home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / CDFIND.BAT < prev    next >
DOS Batch File  |  1995-04-08  |  4KB  |  113 lines

  1. @echo OFF
  2. REM *****************************************************************
  3. REM *** CDfind - Use CEnvi to search this disk, or all disks, for ***
  4. REM *** ver.3    directory matching any given file spec.  Then    ***
  5. REM ***          change to that directory                         ***
  6. REM *****************************************************************
  7.  
  8. CEnviD %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
  9. GOTO CENVI_EXIT
  10.  
  11. main(argc,argv)
  12. {
  13.    if ( 2 != argc  ||  !strcmp("/?",argv[1])  ||  !stricmp("/help",argv[1])  ||  0 == argv[1][0] ) {
  14.       Instructions();
  15.    } else {
  16.       DirCount = FindDirs(argv[1],Dirs);
  17.       if ( !DirCount ) {
  18.          printf("No directory matching \"%s\"!\a\n\n",argv[1]);
  19.          exit(EXIT_FAILURE);
  20.       }
  21.       if ( 1 == DirCount ) {
  22.          printf("1 directory found.\n");
  23.          ChangeToDirectory(Dirs[0]);
  24.       } else {
  25.          printf("%d directories found.\n",DirCount);
  26.          for ( choice = 0; choice < DirCount; choice++ ) {
  27.             printf("cd %s (Y/N) ? ",Dirs[choice]);
  28.             GetResponse:
  29.             Response = toupper(getch());
  30.             if ( !strchr("YN",Response) ) {
  31.                   printf("\a");
  32.                   goto GetResponse;
  33.             }
  34.             printf("%c\n",Response);
  35.             if ( 'Y' == Response ) {
  36.                ChangeToDirectory(Dirs[choice]);
  37.                break;
  38.             }
  39.          }
  40.       }
  41.    }
  42.    printf("\n");
  43. }
  44.  
  45. FindDirs(pSpec,pDirList) // find a directories matching spec and return
  46. {
  47.    // determine spec searching for to see if search all drives, all of one
  48.    // drive, or specified subbdirectories
  49.    NameParts = SplitFileName(pSpec);
  50.    if ( NameParts.dir[0] == 0 ) {
  51.       lDirCount = SearchAllDrives(pSpec,pDirList);
  52.    } else if ( 2 == strlen(NameParts.dir)  &&  NameParts.dir[1] == ':' )
  53.       lDirCount = SearchOneDrive(NameParts.dir[0],pSpec+2,pDirList,0);
  54.    else {
  55.       lDirCount = SearchSubdir(pSpec,pDirList,0);
  56.    }
  57.    return lDirCount;
  58. }
  59.  
  60. SearchSubdir(pSpec,pDirList,pDirCount) // search from SearchSpec for all matching files
  61. {
  62.    lDirCount = pDirCount;
  63.    List = Directory(pSpec,TRUE,FATTR_RDONLY|FATTR_SUBDIR|FATTR_ARCHIVE,FATTR_SUBDIR);
  64.    if ( NULL != List ) {
  65.       lCount = 1 + GetArraySpan(List);
  66.       for ( i = 0; i < lCount; i++ )
  67.          pDirList[lDirCount++] = List[i].name;
  68.    }
  69.    return lDirCount;
  70. }
  71.  
  72. SearchOneDrive(DriveLetter,SearchSpec,pDirList,pDirCount)
  73. {
  74.    sprintf(FullSearchSpec,"%c:\\%s",DriveLetter,SearchSpec);
  75.    return SearchSubDir(FullSearchSpec,pDirList,pDirCount);
  76. }
  77.  
  78. SearchAllDrives(pSearchSpec,pDirList) // look on all valid drives
  79. {
  80.    lDirCount = 0;
  81.    for ( DriveLetter = 'C'; DriveLetter <= 'Z'; DriveLetter++ ) {
  82.       sprintf(DriveCurdir,"%c:.",DriveLetter);
  83.       if ( NULL != FullPath(DriveCurdir) ) {
  84.          lDirCount = SearchOneDrive(DriveLetter,pSearchSpec,pDirList,lDirCount);
  85.       }
  86.    }
  87.    return lDirCount;
  88. }
  89.  
  90. ChangeToDirectory(pDirname)
  91. {
  92.    system("cd %s",pDirname);
  93.    if ( ':' == pDirname[1] )
  94.       system("%c:",pDirname[0]);
  95. }
  96.  
  97. Instructions()
  98. {
  99.    printf("\a\n")
  100.    printf("CDfind.bat - Find directory and change to that directory.\n")
  101.    printf("\n")
  102.    printf("SYNTAX: CDfind <DirSpec>\n")
  103.    printf("\n")
  104.    printf("Where:  DirSpec - Directory specification to match in finding file\n")
  105.    printf("\n")
  106.    printf("Examples: CDfind C:MDOS       Find a MDOS directory on C:\n")
  107.    printf("          CDfind E:UT*        Find directory starting with UT on E:\n")
  108.    printf("          CDfind TOOLS        Find TOOLS directory on any drive\n")
  109.    printf("          CDfind D:\\UTL\\BOZ?  Find BOZ? directory below D:\\UTL\n")
  110. }
  111.  
  112. :CENVI_EXIT
  113.